import pymysql
from pymysql import connect

from common.Conf_API import conf


class get_mysql():
    # def __init__(self, host, port, user, password, ):
    #     # 建立连接
    #     self.conn = pymysql.connect(
    #         # host="120.78.128.25",
    #         host=host,
    #         port=port,
    #         user=user,
    #         password=password,
    #         charset='utf8',
    #         # 将数据返回值设置为字典类型
    #         cursorclass=pymysql.cursors.DictCursor
    #     )

    def __init__(self):
        # 建立连接
        self.conn = pymysql.connect(
            # host="120.78.128.25",
            host=conf.get("mysql", "host"),
            port=conf.getint("mysql", "port"),
            user=conf.get("mysql", "user"),
            password=conf.get("mysql", "password"),
            charset='utf8',
            # 将数据返回值设置为字典类型
            cursorclass=pymysql.cursors.DictCursor
        )

    def find_all(self, sql):
        # 创建游标
        self.cur = self.conn.cursor()
        self.cur.execute(sql)
        res = self.cur.fetchall()
        self.cur.close()
        self.conn.commit()
        return res

    def find_one(self, sql):
        # 创建游标
        self.cur = self.conn.cursor()
        self.cur.execute(sql)
        res = self.cur.fetchone()
        self.cur.close()
        self.conn.commit()
        return res

    def find_count(self, sql):
        # 创建游标
        self.cur = self.conn.cursor()
        self.cur.execute(sql)
        res = self.cur.execute(sql)
        self.cur.close()
        self.conn.commit()
        return res

    def close(self):
        self.conn.close()


mysql = get_mysql()

# if __name__ == "__main__":
#     # print(cursor)
#     sql = "SELECT * from futureloan.member where mobile_phone like '1566666666%'"
#     #
#     # db = get_mysql(
#     #     host=conf.get("mysql", "host"),
#     #     port=conf.getint("mysql", "port"),
#     #     user=conf.get("mysql", "user"),
#     #     password=conf.get("mysql", "password"),
#     # )
#     db = get_mysql()
#     res = db.find_count(sql)
#     print(res)
